package com.api.syh56.shengyunhui.service.impl;

import com.api.syh56.shengyunhui.common.Constants;
import com.api.syh56.shengyunhui.entity.User;
import com.api.syh56.shengyunhui.service.AdminUserService;
import com.api.syh56.shengyunhui.service.UserService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class AdminUserServiceImpl implements AdminUserService {

    @Autowired
    private UserService userService;

    @Override
    public Page<User> select(Short roles, String username, String company, String inviteBy, Integer pageNum,Integer pageSize) {

        Page<User> page = new Page<>(pageNum, pageSize);
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(username)) {
            wrapper.lambda().like(User::getUserName,username);
        }
        if (StringUtils.isNotBlank(company)){
            wrapper.lambda().like(User::getCompany,company);
        }
        if (StringUtils.isNotBlank(inviteBy)) {
            wrapper.lambda().eq(User::getInviteBy,inviteBy);
        }
        if (roles != null) {
            wrapper.lambda().eq(User::getRoles, roles);
        }

        wrapper.lambda().orderByDesc(User::getCreationTime);
        Page<User> userPage = userService.page(page, wrapper);

        return userPage;
    }
}
